#define  _DEBUG_GLOBAL_
#include "config.h"

void debugDataProc(U8 iData)
{
	if (debugInfo.debugMsgLength)
	{
		debugInfo.debugMsgBuff[debugInfo.debugMsgLengthRec++] = iData;

		if (debugInfo.debugMsgLengthRec == debugInfo.debugMsgLength)
		{
			if (0xFE == debugInfo.debugMsgBuff[0])
			{
				//naviBoardDisplayMsg(debugInfo.debugMsgBuff[1],debugInfo.debugMsgBuff[2],&debugInfo.debugMsgBuff[3],debugInfo.debugMsgLength-1-2);
			}
			else if (0xB7 == debugInfo.debugMsgBuff[0])
			{
				//ipcStartEvent(EVENT_GLOBAL_DEBUG_SEND_TO_BT,NULL,&debugInfo.debugMsgBuff[1],debugInfo.debugMsgLength-1);
			}
			//else if (APP_DISC == debugInfo.debugMsgBuff[0])
			{
				//dvdCmdPrintf(&debugInfo.debugMsgBuff[1],debugInfo.debugMsgLength-1);
			}
			//else if (APP_SP_UI == debugInfo.debugMsgBuff[0])
			{
			}
			
			debugInfo.debugMsgLength = 0;
			debugInfo.debugMsgLengthRec = 0;
		}
	}
	else if (4 == debugInfo.debugMsgSync)
	{
		debugInfo.debugMsgSync = 0;
		
		debugInfo.debugMsgLength = iData;
		debugInfo.debugMsgLengthRec = 0;
	}

	if (0xFF == iData)
	{
		debugInfo.debugMsgSync++;
	}
	else
	{
		debugInfo.debugMsgSync = 0;
	}
}

void dealAsciiProc(BYTE ascii)
{
	if ('\r' == ascii || '\n' == ascii)
	{
		if (debugInfo.debugAsciiLength)
		{
			if ('B' == debugInfo.debugAsciiBuff[0] && 'T' == debugInfo.debugAsciiBuff[1])
			{
				debugInfo.debugAsciiBuff[debugInfo.debugAsciiLength] = '\r';
				debugInfo.debugAsciiBuff[debugInfo.debugAsciiLength+1] = '\n';
				//ipcStartEvent(EVENT_GLOBAL_DEBUG_SEND_TO_BT,NULL,&debugInfo.debugAsciiBuff[2],debugInfo.debugAsciiLength);
			}
		}
		debugInfo.debugAsciiSync = 0;
		debugInfo.debugAsciiLength = 0;
	}
	else
	{
		if (5 == debugInfo.debugAsciiSync)
		{
			if (debugInfo.debugAsciiLength < sizeof(debugInfo.debugAsciiBuff))
			{
				debugInfo.debugAsciiBuff[debugInfo.debugAsciiLength++] = ascii;
			}
		}
		else if (
			(0 == debugInfo.debugAsciiSync && 'a' == ascii)
			||
			(1 == debugInfo.debugAsciiSync && 's' == ascii)
			||
			(2 == debugInfo.debugAsciiSync && 'c' == ascii)
			||
			(3 == debugInfo.debugAsciiSync && 'i' == ascii)
			||
			(4 == debugInfo.debugAsciiSync && 'i' == ascii)
		)
		{
			debugInfo.debugAsciiSync++;
			debugInfo.debugAsciiLength = 0;
		}
		else
		{
			debugInfo.debugAsciiSync = 0;
			debugInfo.debugAsciiLength = 0;
		}
	}
}

U8 rrr,ggg,bbb;
U32 xxxSendTime;
void xxxSendProc(void)
{
	
	if (GetTickCount() - xxxSendTime >= T_1S)
	{
		xxxSendTime = GetTickCount();
	}
	return;
}

void cmmdlineProc(U8 c)
{
	static u16 len;
    static u8* pmsg;
	
	FIFO_PUT_ONE(Uart1TxFifo,c);
	
	if (len == 0 )
	{
		if (isalpha(c))
		{
			pmsg = &CMD_Buffer[CMD_Head][0];
			if (pmsg != 0)
			{
				*(pmsg+len) = c;
				len++;
			}
		}
	}
	else
	{
		*(pmsg+len) = c;
		len++;
		if (c =='\r')
		{
			--len;
			*(pmsg+len) = 0;
			CMD_Head = (CMD_Head+1)%CMD_FIFO_LEN;
			len = 0;
		}
		else if (c == '\b')
		{
			--len;
		}
		if(len >= CMD_LINE_BUF)
		{
			len = 0;
		}
	}
}

void debugFreeIdleProc(void)
{
	U8 iRxData;
	if (Uart1_RxEn)
	{
		while (FIFO_GET_ONE(Uart1RxFifo,&iRxData))
		{			
			//debugDataProc(iRxData);
			//dealAsciiProc(iRxData);
			cmmdlineProc(iRxData);
		}
	}
    CMD_Run();
}

void debugInit(void)
{
#if defined(DEBUG_MSG)
	//Uart1Init(256000);
    Uart1Init(115200);
#endif	
	memset(&debugInfo,0,sizeof(DEBUG_INFO));
}
